//
//  CountryViewController.m
//  AroundTheWorld
//
//  Created by dllo on 16/5/19.
//  Copyright © 2016年 IfelseGo an. All rights reserved.
//

#import "CountryViewController.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

#define CELL @"cell"
#define ANMaxSections 100
#define HeaderHight 300

#import "DataHandler.h"

#import "CountryMyCollectionViewCell.h"
#import "CountyDataModel.h"
#import "Hot_cityTableViewCell.h"
#import "New_discountTableViewCell.h"

#import "DestnationCountryDiscountRootVC.h"
#import "DestnationCountryDiscountWonderfulVC.h"
#import "DestnationCountryDiscountSuperVC.h"

#import "ANScrollView.h"
#import "WMPageController.h"
#import "WMPageConst.h"

static NSString *const discountReuse = @"discountReuse";
static NSString *const hot_cityReuse = @"hot_cityReuse";

@interface CountryViewController ()

@property (nonatomic, strong) UITableView *myTabelView;
@property (nonatomic, strong) UIImageView *mapImageV;
@property (nonatomic, strong) UICollectionView *slideCV;
@property (nonatomic, assign) NSInteger myPageNumber;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) NSMutableArray *allArray;
@property (nonatomic, strong) ANScrollView *slideView;

@property (nonatomic, strong) CountyDataModel *datamodel;

@property (nonatomic, strong) UILabel *cnnameLabel;
@property (nonatomic, strong) UILabel *ennameLabel;
@property (nonatomic, strong) UILabel *entryContLabel;

@property (nonatomic, strong) UIButton *massegeButton;
@property (nonatomic, strong) NSMutableArray *hotcityArray;
@property (nonatomic, strong) NSMutableArray *discountArray;
@property (nonatomic, strong) WMPageController *pageController;

@end

@implementation CountryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self getData];
    
    self.myTabelView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.myTabelView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myTabelView];
    
    //成为代理人
    self.myTabelView.delegate = self;
    self.myTabelView.dataSource = self;
    
    //注册
    UINib *nib = [UINib nibWithNibName:@"New_discountTableViewCell" bundle:[NSBundle mainBundle]];
    [self.myTabelView registerNib:nib forCellReuseIdentifier:discountReuse];
    [self.myTabelView registerClass:[Hot_cityTableViewCell class] forCellReuseIdentifier:hot_cityReuse];
    
    //添加头视图
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 300)];
    headerView.backgroundColor = [UIColor whiteColor];
    self.myTabelView.tableHeaderView = headerView;
    
    // Do any additional setup after loading the view.
}
//tempButton
- (void)buttonAtcion{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
//data
-(void)getData{
    self.allArray = [NSMutableArray array];
    
    DataHandler *handler1 = [[DataHandler alloc] init];
    [handler1 getDestinationCounty:self.countyID succeedHandler:^(id result) {
        self.countryArrayM = [NSMutableArray arrayWithArray:result];
         self.datamodel = [self.countryArrayM objectAtIndex:0];
        self.allArray = self.datamodel.photos;
        self.countyID = self.datamodel.countryID;
        self.hotcityArray = self.datamodel.hot_city;
        self.discountArray = self.datamodel.countryNew_discount;
        [self addANSlideImage];
        [self addLabelButton:self.datamodel];
        [self.myTabelView reloadData];
    } failedHandler:^(NSError *error) {
    }];
}

//addButton, Label
- (void)addLabelButton:(CountyDataModel *)model{
    
    UIView *bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 300)];
//    view.backgroundColor=[UIColor blackColor];
    CGFloat a=1.0;
     for (double i=0; i< 150; i++) {
        a = ( i / 150);
        UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 300 - i, WIDTH, 1)];
        view.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:1 - a];
        [bgview addSubview:view];
     }
    bgview.alpha = 0.5;
    [self.myTabelView addSubview:bgview];
    
    self.cnnameLabel = [self addLabel:25];
    self.cnnameLabel.text = model.cnname;
    self.cnnameLabel.frame = CGRectMake(WIDTH / 414 * 40, HeaderHight / 300 * 155, WIDTH / 2, HeaderHight / 300 * 40);
    
    self.ennameLabel = [self addLabel:17];
    self.ennameLabel.text = model.enname;
    self.ennameLabel.frame = CGRectMake(WIDTH / 414 * 40, HeaderHight / 300 * 190, WIDTH / 2, HeaderHight / 300 * 30);
    
    self.entryContLabel = [self addLabel:16];
    self.entryContLabel.text = model.entryCont;
    self.entryContLabel.frame = CGRectMake(WIDTH / 414 * 40, HeaderHight / 300 * 208, WIDTH  - 80, HeaderHight / 300 * 80);
    self.entryContLabel.numberOfLines = 1;
    
    self.massegeButton = [self addButton:@"国家使用信息 >" fontSize:16];
    self.massegeButton.frame = CGRectMake(WIDTH - 160, HeaderHight / 300 * 250, 120, HeaderHight / 300 * 40);
    
//    UIButton *qgB = [self addButton:@"去过" fontSize:14];
//    qgB.frame = CGRectMake(WIDTH - 80, HeaderHight / 300 * 130, 40, HeaderHight / 300 * 70);
//    [qgB setContentVerticalAlignment:UIControlContentVerticalAlignmentBottom];
    
//    UIButton *map = [self addButton:@"setUp_2" fontSize:16];
//    map.frame = CGRectMake(WIDTH - 50, HeaderHight / 300 * 30, 40, HeaderHight / 300 * 35);
////    map.backgroundColor = [UIColor magentaColor];
//    map.alpha = 0.5;
    
    //返回按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(18, 35, 20, 20);
    button.backgroundColor = [UIColor clearColor];
    [button setBackgroundImage:[UIImage imageNamed:@"icon-fanhui 64x64"] forState:UIControlStateNormal]; // icon-fanhui 64x64
    [self.myTabelView addSubview:button];
    [button addTarget:self action:@selector(buttonAtcion) forControlEvents:UIControlEventTouchUpInside];
    
//TEMP返回按钮****************************
//    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
//    button1.frame = CGRectMake(18, 100, 20, 20);
//    button1.backgroundColor = [UIColor greenColor];
//    [self.myTabelView addSubview:button1];
//    [button1 addTarget:self action:@selector(buttonAtciona) forControlEvents:UIControlEventTouchUpInside];
}

- (void)buttonAtciona{
    WMPageController *pageController = [self getPages];
     [self.navigationController pushViewController:pageController animated:YES];
     UINavigationController *CommunityNC = [[UINavigationController alloc] initWithRootViewController:pageController];
//    [button1 addTarget:self action:@selector(buttonAtciona) forControlEvents:UIControlEventTouchUpInside];


    [self presentViewController:CommunityNC animated:YES completion:^{
        
    }];
}

- (WMPageController *) getPages {
    [self addPageNotifications];
    
    //WMPageController中包含的页面数组
    NSArray *controllers = [NSArray arrayWithObjects:[DestnationCountryDiscountSuperVC class],[DestnationCountryDiscountWonderfulVC class],nil];
    
    //WMPageController控件的标题数组
    NSArray *titles = [NSArray arrayWithObjects:@"超值自由行", @"精彩当地游", nil];
    //用上面两个数组初始化WMPageController对象
    WMPageController *pageController = [[WMPageController alloc] initWithViewControllerClasses:controllers andTheirTitles:titles];
    //设置WMPageController每个标题的宽度
    pageController.menuItemWidth = 100;
    //设置WMPageController标题栏的高度
    pageController.menuHeight = 50;
    pageController.titleSizeSelected = 16;
    pageController.titleSizeNormal = 16;
    pageController.titleColorNormal = [UIColor grayColor];
    pageController.selectIndex = 0;
    pageController.menuViewStyle = WMMenuViewStyleLine;
    pageController.postNotification = YES;
    pageController.contyId = self.countyID;
//    pageController.menuBGColor = [UIColor colorWithRed:53 / 255.0 green:176 /255.0 blue:131 / 255.0 alpha:1.0];
    //设置WMPageController选中的标题的颜色
    pageController.titleColorSelected = [UIColor colorWithRed:53 / 255.0 green:176 /255.0 blue:131 / 255.0 alpha:1.0];
    pageController.progressColor = [UIColor colorWithRed:53 / 255.0 green:176 /255.0 blue:131 / 255.0 alpha:1.0];
    
//    DestnationCountryDiscountSuperVC *superVC = [[DestnationCountryDiscountSuperVC alloc] init];
//    superVC.countyID = pageController.contyId;

    self.pageController = pageController;
    return pageController;
}

- (UILabel *)addLabel:(NSInteger )fontSize{
    UILabel *label = [[UILabel alloc] init];
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor whiteColor];
    [self.myTabelView addSubview:label];
    label.font = [UIFont systemFontOfSize:fontSize];
    return label;
}
- (UIButton *)addButton:(NSString *)buttonText fontSize:(NSInteger )fontSize{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor clearColor];
    button.titleLabel.textColor = [UIColor whiteColor];
    button.titleLabel.font = [UIFont systemFontOfSize:fontSize];
    [button setTitle:buttonText forState:UIControlStateNormal];
    [self.myTabelView addSubview:button];
    return button;
}

//分区的个数
-(NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
    if (self.discountArray.count == 0) {
        return 1;
    }
    return 2;
}

//分区头高度
-(CGFloat )tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section
{
    return 50;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{ //背景颜色
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor whiteColor];
    
    //分隔线
    UILabel *FGX = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH , 10)];
    FGX.backgroundColor = [UIColor colorWithRed:232 / 255.0 green:230 / 255.0 blue:233 / 255.0 alpha:1.0];
    [view addSubview:FGX];
    
    //标题
    UILabel *text = [[UILabel alloc] initWithFrame:CGRectMake(20, 15, WIDTH -30 , 30)];
    [view addSubview:text];
    text.textColor = [UIColor grayColor];
    
//    UILabel *CKQB = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH - 80, 15, 80 , 30)];
//    CKQB.text = @"查看全部 >";
//    CKQB.textColor = [UIColor grayColor];
//    CKQB.font = [UIFont systemFontOfSize:12];
//    [view addSubview:CKQB];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"查看全部 >" forState:UIControlStateNormal];
    button.frame = CGRectMake(WIDTH - 110, 15, 100 , 30);
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [view addSubview:button];
    
    
    
    
    //文字定义
    if (section == 0) {
        text.text = @"城市";
        [button addTarget:self action:@selector(CSbuttonAction:) forControlEvents:UIControlEventTouchUpInside];
    }else
    {
        text.text = @"超值自由行";
        [button addTarget:self action:@selector(CZJYXbuttonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return view;
}
//超值自由行 ,查看全部点击方法
- (void)CZJYXbuttonAction:(UIButton *)button
{
    
    WMPageController *pageController = [self getPages];
    [self.navigationController pushViewController:pageController animated:YES];
    UINavigationController *CommunityNC = [[UINavigationController alloc] initWithRootViewController:pageController];
 

//    DestnationCountryDiscountSuperVC *superVC = [[DestnationCountryDiscountSuperVC alloc] init];
//    DestnationCountryDiscountWonderfulVC *wonderfulVC = [[DestnationCountryDiscountWonderfulVC alloc] init];
//    superVC.countyID = self.countyID;
//    wonderfulVC.countyID = self.countyID;
    
    
    
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"contyID" object:self.countyID];   //单利  发送
    NSLog(@"countyID======3==========%@",self.countyID);
    
    [self presentViewController:CommunityNC animated:YES completion:^{
//        self.myBlock(self.countyID);
    }];

}
//城市 查看全部
- (void)CSbuttonAction:(UIButton *)button
{
    
}


//cell高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //分区1
    if (indexPath.section == 0) {
     return 265;
    }
    //分区2
    return 100;
}

//cell数
- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    return self.discountArray.count;
}

//建cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        Hot_cityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:hot_cityReuse];
        cell.smallArray = self.hotcityArray;
        return cell;
    }
    New_discountTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:discountReuse];
    New_discountModel *model = [self.discountArray objectAtIndex:indexPath.row];
    cell.myModel = model;
    return cell;
}

//点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        
    }
}


//轮播图
- (void)addANSlideImage {
    if (_slideView == nil) {
        ANScrollView *scollView = [ANScrollView new];
        scollView.height = 300;
        self.slideView = scollView;
        __block CountryViewController *countryVC = self;
        self.slideView.myBlock = ^(NSInteger pageNumber){
            countryVC.myPageNumber = pageNumber;
            //            NSLog(@"PageNumber====1====%ld",pageNumber);
        };

    }
    [self.slideView showInView:self.myTabelView withContents:self.allArray];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

// Page Controller Notification

- (void)addPageNotifications
{
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(didAddToSuperNotification:) name:WMControllerDidAddToSuperViewNotification object:nil];
}

- (void)didAddToSuperNotification:(NSNotification *)notification
{
    DestnationCountryDiscountSuperVC *controller = (DestnationCountryDiscountSuperVC *) [self.pageController.displayVC objectForKey:@(0)];
    controller.countyID = self.countyID;
    
    controller = (DestnationCountryDiscountSuperVC *) [self.pageController.displayVC objectForKey:@(1)];
    controller.countyID = self.countyID;
}

@end
